﻿Imports System.Data.SqlClient
Imports UTNComputers.clsVentas_Detalle
Imports UTNComputers.clsCatalogo
Imports UTNComputers.daoCatalogo
Public Class daoVenta_Detalle
    'Funcion para el alta de Detalle de Venta'
    Public Function Alta(ByVal Venta_Detalle As clsVentas_Detalle) As Boolean
        Dim Cn As New SqlConnection
        Dim Cm As New SqlCommand

        Try
            Cn.ConnectionString = My.Resources.Conexion
            Cn.Open()
            Cm.CommandType = CommandType.StoredProcedure
            Cm.CommandText = "SP_ALTA_VENTA_DETALLE"

            Cm.Parameters.Add("@vtadte_id", SqlDbType.Int)
            Cm.Parameters.Add("@vta_id", SqlDbType.Int)
            Cm.Parameters.Add("@prod_id", SqlDbType.Int)
            Cm.Parameters.Add("@nro_serie", SqlDbType.VarChar)
            Cm.Parameters.Add("@vtadte_cantidad", SqlDbType.Int)
            Cm.Parameters.Add("@Cod_Suc", SqlDbType.Int)

            Cm.Parameters("@vtadte_id").Value = Venta_Detalle.Vtadte_Id
            Cm.Parameters("@vta_id").Value = Venta_Detalle.Vta_Id
            Cm.Parameters("@prod_id").Value = Venta_Detalle.Prod_Id
            Cm.Parameters("@nro_serie").Value = Venta_Detalle.Nro_Serie
            Cm.Parameters("@vtadte_cantidad").Value = Venta_Detalle.Vtadte_Cantidad
            Cm.Parameters("@Cod_Suc").Value = Venta_Detalle.Cod_Suc

            Cm.Connection = Cn

            Cm.ExecuteNonQuery()
            Cn.Close()
            Return True

        Catch ex As Exception
            Return False
        Finally
            Cn.Dispose()
            Cm.Dispose()
        End Try
    End Function

    'Funcion para la modificacion detalle de Presupuesto'
    Public Function Modificar(ByVal Venta_Detalle As clsVentas_Detalle) As Boolean
        Dim Cn As New SqlConnection
        Dim Cm As New SqlCommand

        Try
            Cn.ConnectionString = My.Resources.Conexion
            Cn.Open()
            Cm.CommandType = CommandType.StoredProcedure
            Cm.CommandText = "SP_ALTA_MODI_DETALLE"

            Cm.Parameters.Add("@vtadte_id", SqlDbType.Int)
            Cm.Parameters.Add("@vta_id", SqlDbType.Int)
            Cm.Parameters.Add("@prod_id", SqlDbType.Int)
            Cm.Parameters.Add("@nro_serie", SqlDbType.VarChar)
            Cm.Parameters.Add("@vtadte_cantidad", SqlDbType.Int)
            Cm.Parameters.Add("@Cod_Suc", SqlDbType.Int)

            Cm.Parameters("@vtadte_id").Value = Venta_Detalle.Vtadte_Id
            Cm.Parameters("@vta_id").Value = Venta_Detalle.Vta_Id
            Cm.Parameters("@prod_id").Value = Venta_Detalle.Prod_Id
            Cm.Parameters("@nro_serie").Value = Venta_Detalle.Nro_Serie
            Cm.Parameters("@vtadte_cantidad").Value = Venta_Detalle.Vtadte_Cantidad
            Cm.Parameters("@Cod_Suc").Value = Venta_Detalle.Cod_Suc

            Cm.Connection = Cn

            Cm.ExecuteNonQuery()
            Cn.Close()
            Return True

        Catch ex As Exception
            Return False
        Finally
            Cn.Dispose()
            Cm.Dispose()
        End Try
    End Function

    'Funcion para Traer un Detalle de Venta por ID'
    Public Function TraeDetalleVenta(ByVal vtadte_id As Integer) As clsVentas_Detalle
        Dim Cn As New SqlClient.SqlConnection
        Dim Cm As New SqlClient.SqlCommand
        Dim Dr As SqlClient.SqlDataReader
        Dim Venta_Detalle As New clsVentas_Detalle()

        Dim Conf As New clsConfiguraciones()
        Dim dao_Conf As New daoConfiguraciones()
        Conf = dao_Conf.TraeConfiguraciones()

        Try
            Cn.ConnectionString = My.Resources.Conexion
            Cn.Open()
            Cm.CommandType = CommandType.StoredProcedure
            Cm.CommandText = "SP_TRAER_VENTA_DETALLE"

            Cm.Parameters.Add("@vtadte_id", SqlDbType.Int)
            Cm.Parameters("@vtadte_id").Value = vtadte_id

            Cm.Parameters.Add("@Cod_Suc", SqlDbType.Int)
            Cm.Parameters("@Cod_Suc").Value = Conf.Cod_Suc

            Cm.Connection = Cn
            Dr = Cm.ExecuteReader
            If Dr.HasRows = True Then
                While Dr.Read
                    Venta_Detalle.Vtadte_Id = Dr.GetInt32(0)
                    Venta_Detalle.Vta_Id = Dr.GetInt32(1)
                    Venta_Detalle.Prod_Id = Dr.GetInt32(2)
                    If Dr.IsDBNull(3) Then
                        Venta_Detalle.Nro_Serie = "-"
                    Else
                        Venta_Detalle.Nro_Serie = Dr.GetString(3).Trim.ToUpper
                    End If

                    Venta_Detalle.Vtadte_Cantidad = Dr.GetInt32(4)
                    Venta_Detalle.Cod_Suc = Dr.GetInt32(5)

                    Dim Catalogo_Dao As New daoCatalogo()
                    Venta_Detalle.Producto = Catalogo_Dao.TraeArticulo(Venta_Detalle.Prod_Id)
                    Catalogo_Dao.Dispose()
                End While

                Dr.Close()
                Cn.Close()
            End If
            Return Venta_Detalle
        Catch ex As Exception
            Throw New Exception(ex.Message)
        Finally
            Cn.Dispose()
            Cm.Dispose()
        End Try
    End Function

    'Funcion para Traer Todos los detalles de una Venta por Nº Venta'
    Public Function TraeDetallesVenta(ByVal vta_id As Integer) As List(Of clsVentas_Detalle)
        Dim Cn As New SqlClient.SqlConnection
        Dim Cm As New SqlClient.SqlCommand
        Dim Dr As SqlClient.SqlDataReader
        Dim ListaDetalle_Venta As New List(Of clsVentas_Detalle)

        Dim Conf As New clsConfiguraciones()
        Dim dao_Conf As New daoConfiguraciones()
        Conf = dao_Conf.TraeConfiguraciones()

        Try
            Cn.ConnectionString = My.Resources.Conexion
            Cn.Open()
            Cm.CommandType = CommandType.StoredProcedure
            Cm.CommandText = "SP_TRAER_VENTA_DETALLE_X_VTA_ID"

            Cm.Parameters.Add("@vta_id", SqlDbType.Int)
            Cm.Parameters("@vta_id").Value = vta_id

            Cm.Parameters.Add("@Cod_Suc", SqlDbType.Int)
            Cm.Parameters("@Cod_Suc").Value = Conf.Cod_Suc

            Cm.Connection = Cn
            Dr = Cm.ExecuteReader
            If Dr.HasRows = True Then
                While Dr.Read
                    Dim Venta_Detalle As New clsVentas_Detalle()
                    Venta_Detalle.Vtadte_Id = Dr.GetInt32(0)
                    Venta_Detalle.Vta_Id = Dr.GetInt32(1)
                    Venta_Detalle.Prod_Id = Dr.GetInt32(2)
                    If Dr.IsDBNull(3) Then
                        Venta_Detalle.Nro_Serie = "-"
                    Else
                        Venta_Detalle.Nro_Serie = Dr.GetString(3).Trim.ToUpper
                    End If

                    Venta_Detalle.Vtadte_Cantidad = Dr.GetInt32(4)
                    Venta_Detalle.Cod_Suc = Dr.GetInt32(5)

                    Dim Catalogo_Dao As New daoCatalogo()
                    Venta_Detalle.Producto = Catalogo_Dao.TraeArticulo(Venta_Detalle.Prod_Id)
                    Catalogo_Dao.Dispose()

                    ListaDetalle_Venta.Add(Venta_Detalle)
                    Venta_Detalle.Dispose()
                End While

                Dr.Close()
                Cn.Close()
            End If
            Return ListaDetalle_Venta
        Catch ex As Exception
            Throw New Exception(ex.Message)
        Finally
            Cn.Dispose()
            Cm.Dispose()
        End Try
    End Function

    'Funcion para Traer Todos los detalles de una Venta por Nº Venta y Cod Suc'
    Public Function TraeDetallesVentaCodSuc(ByVal vta_id As Int32, ByVal Cod_Suc As Int32) As List(Of clsVentas_Detalle)
        Dim Cn As New SqlClient.SqlConnection
        Dim Cm As New SqlClient.SqlCommand
        Dim Dr As SqlClient.SqlDataReader
        Dim ListaDetalle_Venta As New List(Of clsVentas_Detalle)


        Try
            Cn.ConnectionString = My.Resources.Conexion
            Cn.Open()
            Cm.CommandType = CommandType.StoredProcedure
            Cm.CommandText = "SP_TRAER_VENTA_DETALLE_X_VTA_ID"

            Cm.Parameters.Add("@vta_id", SqlDbType.Int)
            Cm.Parameters("@vta_id").Value = vta_id

            Cm.Parameters.Add("@Cod_Suc", SqlDbType.Int)
            Cm.Parameters("@Cod_Suc").Value = Cod_Suc

            Cm.Connection = Cn
            Dr = Cm.ExecuteReader
            If Dr.HasRows = True Then
                While Dr.Read
                    Dim Venta_Detalle As New clsVentas_Detalle()
                    Venta_Detalle.Vtadte_Id = Dr.GetInt32(0)
                    Venta_Detalle.Vta_Id = Dr.GetInt32(1)
                    Venta_Detalle.Prod_Id = Dr.GetInt32(2)
                    If Dr.IsDBNull(3) Then
                        Venta_Detalle.Nro_Serie = "-"
                    Else
                        Venta_Detalle.Nro_Serie = Dr.GetString(3).Trim.ToUpper
                    End If

                    Venta_Detalle.Vtadte_Cantidad = Dr.GetInt32(4)
                    Venta_Detalle.Cod_Suc = Dr.GetInt32(5)

                    Dim Catalogo_Dao As New daoCatalogo()
                    Venta_Detalle.Producto = Catalogo_Dao.TraeArticuloCodSuc(Venta_Detalle.Prod_Id, Cod_Suc)
                    Catalogo_Dao.Dispose()

                    ListaDetalle_Venta.Add(Venta_Detalle)
                    Venta_Detalle.Dispose()
                End While

                Dr.Close()
                Cn.Close()
            End If
            Return ListaDetalle_Venta
        Catch ex As Exception
            Throw New Exception(ex.Message)
        Finally
            Cn.Dispose()
            Cm.Dispose()
        End Try
    End Function

    '---------------------------------------------------------------------
    ' Constructor de la Clase
    '---------------------------------------------------------------------
    Public Sub New()

        MyBase.New()

    End Sub

    '---------------------------------------------------------------------
    ' Destructor de la Clase
    '---------------------------------------------------------------------
    Public Sub Dispose()

        MyBase.Finalize()

    End Sub

End Class
